<?php
namespace app\home\controller;

use think\Controller;
use think\Db;
use app\Home\model;
use think\Request;
use think\Session;

class Details extends Controller{

    //课程详情页首页
    public function index(){

        $getRequest = Request::instance()->get();
        $id = empty($getRequest['id'])?14:$getRequest['id'];

        //视频
        $m = new model\Details();
        $details = $m->index($id);


        //推荐视频
        $res = Db::table('courses')->where('courses_del',0)->limit(4)->select();



        //评论
        $videocom = Db::table('videocom')
                ->alias("vid")
                ->where('courses_id',$id)
                ->join('users u','vid.user_id = u.user_id')
                ->limit(15)
                ->order('comment_time desc')
                ->select();

        //判断付款
        $orders = Db::table("orders")
            ->where("user_id",Session::get("USERS.user_id"))
            ->where("courses_id",$id)
            ->find();

        $orders = is_null($orders)?1:0;


        //判断收藏
        $collection = Db::table("collect")
                ->where("currentUser_id",Session::get("USERS.user_id"))
                ->where("courses_id",$id)
                ->find();
        $collection = is_null($collection)?1:0;

        $sessionUser = Session::get("USERS");

        $this->assign("sessionUser",$sessionUser);
        $this->assign("details",$details);
        $this->assign("videocom",$videocom);
        $this->assign("res",$res);
        $this->assign("collection",$collection);
        $this->assign("orders",$orders);

        return $this->fetch();

    }



    //评论处理
    public function videOcom (){

        $m = new model\Details;
        $videocom = $m->videOcom();


        if($videocom != 1){

            $data = "";
            foreach($videocom as $k=>$v){

                $data .= "<li class=\"pl-list clearfix\">";
                $data .= "<div class=\"pl-list-avator\">";
                $data .= "<a href=\"javascript:;\" target=\"_blank\">";
                if(empty($v['user_photo'])){
                    $data .= "<img width=\"40\" height=\"40\" src=\"/static/home/common/img/akari.jpg\" title=\"︿(￣︶￣)︿他很懒\">";
                }else{
                    $data .= "<img width=\"40\" height=\"40\" src=\"{$v['user_photo']}\" title=\"{$v['user_name']}\" >";
                }
                $data .= "</a>";
                $data .= "</div>";
                $data .= "<div class=\"pl-list-main\">";
                $data .= "<div class=\"pl-list-nick\">";
                if(empty($v['user_name'])){
                    $data .= "<a href=\"javascript:;\" target=\"_blank\">!!!∑(ﾟДﾟノ)ノ他懒的名字都忘了</a>";
                }else{
                    $data .= "<a href=\"javascript:;\" target=\"_blank\">{$v['user_name']}</a>";
                }
                $data .= "</div>";
                $data .= "<div class=\"pl-list-content\">{$v['comment_text']}</div>";
                $data .= "<div class=\"pl-list-btm clearfix\">";

                $data .= "<span class=\"pl-list-time l\">刚刚</span>";
                $data .= "</div>";
                $data .= "</div>";
                $data .= "</li>";
            }
        }else{
            $data = 1;
        }
        echo json_encode($data);

    }

    //收藏
    public function collection(){

        $m = new model\Details;
        $collection = $m->collection();

        echo json_encode($collection);


    }

}